from PyQt5.QtWidgets import *
import sys


class QRadioButtonDemo(QWidget):
    def __init__(self):
        super(QRadioButtonDemo, self).__init__()
        self.iniUI()

    def iniUI(self):
        self.setWindowTitle('QPushButton Demo')
        self.layout = QHBoxLayout()
        self.button1 = QRadioButton('单选按钮1')
        #  默认是选中状态
        self.button1.setCheckable(True)
        self.button1.toggled.connect(self.buttonState)
        self.layout.addWidget(self.button1)

        self.button2 = QRadioButton('单选按钮2')
        #  默认是选中状态
        self.button2.setCheckable(True)
        self.button2.toggled.connect(self.buttonState)
        self.layout.addWidget(self.button2)

        self.setLayout(self.layout)
        self.resize(300, 120)


    def buttonState(self):
        #  self.sender 获取当前点击的按钮
        radioButton = self.sender()
        if radioButton.isChecked() == True:
            print(f"{radioButton.text()}被选中")
        else:
            print(f"{radioButton.text()}取消选中")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QRadioButtonDemo()
    main.show()
    sys.exit(app.exec_())
